//存放行校核的MAP
var rowValidationMap = new Map();
//存放列校核的MAP
var colValidationMap = new Map();
//存放单元格校核的MAP
var cellValidationMap = new Map();
//存放表间校核的MAP
var tableValidationMap = new Map();

function closeValidationDiv(){
	DivSetVisibleValidation(false);
}
//表内表间校核的TAB切换
function jspShowTabValidation(liId){
	$("#pop-up_tag_a_validation").attr("class","pop-up_tag_hov");
	$("#pop-up_tag_b_validation").attr("class","pop-up_tag_hov");
	$("#"+liId).attr("class","pop-up_tag_a");
	
	$("#pop-up_tag_a_validation_div").hide();
	$("#pop-up_tag_b_validation_div").hide();
	$("#"+liId+"_div").show();
}

//表内行列单元格校核
function jspShowTabValidationNei(liId){
	$("#pop-up_tag_a_validation_nei").attr("class","pop-up_tag_validation_hov");
	$("#pop-up_tag_b_validation_nei").attr("class","pop-up_tag_validation_hov");
	$("#pop-up_tag_c_validation_nei").attr("class","pop-up_tag_validation_hov");
	
	$("#"+liId).attr("class","pop-up_tag_validation_a");
	$("#pop-up_tag_a_validation_nei_div").hide();
	$("#pop-up_tag_b_validation_nei_div").hide();
	$("#pop-up_tag_c_validation_nei_div").hide();
	$("#"+liId+"_div").show();
}

//根据用户填写的内容，然后进行转换，增加一条行校核
function addRowValidationFormula(){
	var validationFormulaRow = $("#aRowValidationFormula").text();
	$.ajax({
		url:ctx + '/reportTemplateValidationController/rowValidationChange/' + validationFormulaRow,
		type:'POST',
		cache:false,
		success:function(data){
			if(data[0] == "公式错误"){
				alert("公式错误");
				return false;
			}else{
				var hangjiaoheMapKey = randomString(true, 1, 32);
				var tableTr = handleDataHangJiaohe(hangjiaoheMapKey,data[0]);
				$("#hangValidationTable").append(tableTr);
				var hangjiaoheShiti = new HangjiaoheShiti(data[1],data[0],"1");
				rowValidationMap.put(hangjiaoheMapKey,hangjiaoheShiti);
				$("#aRowValidationFormula").val("");
			}
		}
	});
}

//行校核实体(展示公式，公式类型)
function HangjiaoheShiti(rowGongshi,rowGongshiShow,jiaohetype){
	this.rowGongshi=rowGongshi;
	this.rowGongshiShow=rowGongshiShow;
	this.jiaohetype=jiaohetype;
}

function handleDataHangJiaohe(hangjiaoheMapKey,rowGongshi){
	var tr = '<tr>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + rowGongshi + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2" id="' + hangjiaoheMapKey + '"><a href="#" onclick="deleteHangjiaoheTableTr(this)">删除</a>';
    tr += '<td style="display:none">' + hangjiaoheMapKey + '</td>';
    tr += '</tr>';
    return tr; 
}

//删除一行
function deleteHangjiaoheTableTr(obj){
	var id=$(obj).parent().attr('id');
	//删除表格里面的TR
	$('#'+id).parent().remove();
	//删除MAP里面的行计算
	rowValidationMap.remove(id);
}

//根据用户填写的内容，然后进行转换，增加一条列校核
function addColValidationFormula(){
	var validationFormulaCol = $("#aColValidationFormula").text();
	$.ajax({
		url:ctx + '/reportTemplateValidationController/colValidationChange/' + validationFormulaCol,
		type:'POST',
		cache:false,
		success:function(data){
			if(data[0] == "公式错误"){
				alert("公式错误");
				return false;
			}else{
				var liejiaoheMapKey = randomString(true, 1, 32);
				var tableTr = handleDataLieJiaohe(liejiaoheMapKey,data[1]);
				$("#lieValidationTable").append(tableTr);
				var liejiaoheShiti = new LiejiaoheShiti(data[1],data[0],"2");
				colValidationMap.put(liejiaoheMapKey,liejiaoheShiti);
				$("#aColValidationFormula").val("");
			}
		}
	});
}

//列校核实体(展示公式，数据库后台存的公式，公式类型)
function LiejiaoheShiti(colGongshiShow,colGongshiReal,jiaohetype){
	this.colGongshiShow=colGongshiShow;
	this.colGongshiReal=colGongshiReal;
	this.jiaohetype=jiaohetype;
}

function handleDataLieJiaohe(liejiaoheMapKey,colGongshiShow){
	var tr = '<tr>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + colGongshiShow + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2" id="' + liejiaoheMapKey + '"><a href="#" onclick="deleteLiejiaoheTableTr(this)">删除</a>';
    tr += '<td style="display:none">' + liejiaoheMapKey + '</td>';
    tr += '</tr>';
    return tr; 
}
//删除一行
function deleteLiejiaoheTableTr(obj){
	var id=$(obj).parent().attr('id');
	//删除表格里面的TR
	$('#'+id).parent().remove();
	//删除MAP里面的行计算
	colValidationMap.remove(id);
}
//根据用户填写的内容，然后进行转换，增加一条列校核
function addCellValidationFormula(){
	var validationFormulaCell = $("#aCellValidationFormula").text();
	$.ajax({
		url:ctx + '/reportTemplateValidationController/cellValidationChange/' + validationFormulaCell,
		type:'POST',
		cache:false,
		success:function(data){
			if(data[0] == "公式错误"){
				alert("公式错误");
				return false;
			}else{
				var celljiaoheMapKey = randomString(true, 1, 32);
				var tableTr = handleDataCellJiaohe(celljiaoheMapKey,data[1]);
				$("#cellValidationTable").append(tableTr);
				var celljiaoheShiti = new CelljiaoheShiti(data[1],data[0],"3");
				cellValidationMap.put(celljiaoheMapKey,celljiaoheShiti);
				$("#aCellValidationFormula").val("");
			}
		}
	});
}

//单元格校核实体(展示公式，数据库后台存的公式，公式类型)
function CelljiaoheShiti(cellGongshiShow,cellGongshiReal,jiaohetype){
	this.cellGongshiShow=cellGongshiShow;
	this.cellGongshiReal=cellGongshiReal;
	this.jiaohetype=jiaohetype;
}

function handleDataCellJiaohe(celljiaoheMapKey,cellGongshiShow){
	var tr = '<tr>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellGongshiShow + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2" id="' + celljiaoheMapKey + '"><a href="#" onclick="deleteCelljiaoheTableTr(this)">删除</a>';
    tr += '<td style="display:none">' + celljiaoheMapKey + '</td>';
    tr += '</tr>';
    return tr; 
}

//删除一行
function deleteCelljiaoheTableTr(obj){
	var id=$(obj).parent().attr('id');
	//删除表格里面的TR
	$('#'+id).parent().remove();
	//删除MAP里面的行计算
	cellValidationMap.remove(id);
}

//保存校核公式
function saveValidationFormula(){
	$("#but_save_validation").attr('disabled','true');
	var templateId = $("#templateId").val();
	var rowValidationFormula=JSON.stringify(rowValidationMap);
	var colValidationFormula=JSON.stringify(colValidationMap);
	var cellValidationFormula=JSON.stringify(cellValidationMap);
	var tableValidationFormula = JSON.stringify(tableValidationMap);
	$.ajax({
	   type: "POST",
	   url: ctx+"/reportTemplateValidationController/saveValidationFormula/"+templateId,
	   data: {rowValidationFormula:rowValidationFormula,colValidationFormula:colValidationFormula,cellValidationFormula:cellValidationFormula,tableValidationFormula:tableValidationFormula},
	   success: function(){
		   alert("保存成功");
		   $('#but_save_validation').removeAttr("disabled"); 
	   }
	}); 
}
//得到所有的行内校核公式
function getAllTemplateInValidationFormula(){
	var templateId = $("#templateId").val();
	$.ajax({
		url:ctx+'/reportTemplateValidationController/getAllTemplateInValidationFormula/'+templateId,
		type:'POST',
		cache:false,
		success:function(data){
			$("#hangValidationTable tr:gt(0)").remove();
			$("#lieValidationTable tr:gt(0)").remove();
			$("#cellValidationTable tr:gt(0)").remove();
			$("#biaojianValidationFormulaTable tr:gt(0)").remove();
		    $.each(data,function(idx,item){
		    	//判断当前是行校核、列校核还是单元格校核，分别加到各个Table下面
		    	if(item.validationType == '1'){
		    		var tableTr = handleDataHangJiaohe(item.validationId,item.validationFormulaShow);
					$("#hangValidationTable").append(tableTr);
					var hangjiaoheShiti = new HangjiaoheShiti(item.validationFormula,item.validationFormulaShow,item.validationType);
					rowValidationMap.put(item.validationId,hangjiaoheShiti);
		    	}else if(item.validationType == '2'){
		    		var tableTr = handleDataLieJiaohe(item.validationId,item.validationFormulaShow);
					$("#lieValidationTable").append(tableTr);
					var liejiaoheShiti = new LiejiaoheShiti(item.validationFormulaShow,item.validationFormula,item.validationType);
					colValidationMap.put(item.validationId,liejiaoheShiti);
		    	}else if(item.validationType == '3'){
		    		var tableTr = handleDataCellJiaohe(item.validationId,item.validationFormulaShow);
					$("#cellValidationTable").append(tableTr);
					var celljiaoheShiti = new CelljiaoheShiti(item.validationFormulaShow,item.validationFormula,item.validationType);
					cellValidationMap.put(item.validationId,celljiaoheShiti);
		    	}else if(item.validationType == '4'){
		    		var tableTr = handleDataTableJiaohe(item.validationId,item.validationFormulaShow);
					$("#biaojianValidationFormulaTable").append(tableTr);
					var tablejiaoheShiti = new TablejiaoheShiti(item.validationFormulaShow,item.validationFormula,item.validationType);
					tableValidationMap.put(item.validationId,tablejiaoheShiti);
		    	}else{}
			});
		}
	});
}

var allBiaojianValidationTr = "";
//得到该版本下的所有报表，并拼好字符串
function getAllTemplateName(){
	var annualId = $("#annualId").val();
	var biaojianValidationFirstTr = "";
	var biaojianValidationTr = "";
	$.ajax({
		url:ctx+'/reportDesignController/getAllTemplateInfo/'+annualId,
		type:'POST',
		cache:false,
		success:function(data){
		    $.each(data,function(idx,item){
		    	biaojianValidationTr += "<option value='"+item.templateId+"'>" + item.templateName + "</option>";
			});
		    biaojianValidationFirstTr ="<option value='0'>请选择</option>";
		    allBiaojianValidationTr = biaojianValidationFirstTr + biaojianValidationTr;
		}
	});
}

function handleDataBiaojianJiaoheRight(){
	var templateSelect = "<select name='templateSelectName'>"+allBiaojianValidationTr+"</select>";
	var expressionText="<input name='expressionTextName' type='text' value=''/>"
	var tr = '<tr>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + templateSelect + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2" >' + expressionText + '</td>';
    tr += '<td align="center" valign="middle" class="td_grey2" ><a href="javascript:;" onclick="deleteATableValidationRight(this);">删除</a></td>';
    tr += '</tr>';
    return tr; 
}
//右边的表达式再增加一行
function addBiaojianJiaoheRight(){
	var tableTr = handleDataBiaojianJiaoheRight();
	$("#biaojianValidationTable").append(tableTr);
}
//根据表间校核的配置，得到公式左边，公式右边，然后进行转换，生成表间校核公式
function createTableValidationFormula(){
	var leftTableValidationFormulaStr = $("#validationFormulaLeftText").val();
	if(leftTableValidationFormulaStr.indexOf("~") >= 0 && leftTableValidationFormulaStr.indexOf(":") >= 0&& leftTableValidationFormulaStr.indexOf("：") >= 0){
		alert("公式左边不正确");
		return false;
	}
	
	if($("#relationSelectName").val() == 0){
		alert("请选择关系");
		return false;
	}
	var tableLength = $("#biaojianValidationTable tr").length;
	if(tableLength < 2){
		alert("请增加右边公式");
		return false;
	}
	var valudationFormulaSymbol = $("#valudationFormulaSymbol").val();
	
	var templateSelectValueArray = document.getElementsByName("templateSelectName");
	var expressionTextValueArray = document.getElementsByName("expressionTextName");
	var relationSelectName = $("#relationSelectName").val();
	var templateSelectValueStr = "";
	var expressionTextValueStr = "";
	for(var a = 0; a < templateSelectValueArray.length; a++){
		if(templateSelectValueArray[a].value == '0'){
			alert("请选择报表");
			return false;
		}else{
			templateSelectValueStr += templateSelectValueArray[a].value + ",";
		}
	}
	for(var b = 0; b < expressionTextValueArray.length; b++){
		if(expressionTextValueArray[b].value == '' || expressionTextValueArray[b].value == null){
			alert("请填写公式");
			return false;
		}else{
			expressionTextValueStr += expressionTextValueArray[b].value + ",";
		}
	}
	templateSelectValueStr = templateSelectValueStr.substring(0, templateSelectValueStr.length-1);
	expressionTextValueStr = expressionTextValueStr.substring(0, expressionTextValueStr.length-1);
	var templateId = $("#templateId").val();
	
	$.ajax({
		url:ctx+"/reportTemplateValidationController/getATableValidationFormula/"+templateId,
		type:'POST',
		cache:false,
		data: {leftTableValidationFormulaStr:leftTableValidationFormulaStr,valudationFormulaSymbol:valudationFormulaSymbol,templateSelectValueStr:templateSelectValueStr,expressionTextValueStr:expressionTextValueStr,relationSelectName:relationSelectName},
		success:function(data){
			$.each(data,function(idx,item){
				var tablejiaoheMapKey = randomString(true, 1, 32);
				var tablejiaoheShiti = new TablejiaoheShiti(item.showTableValidationFormula,item.realTableValidationFormula,"4");
				tableValidationMap.put(tablejiaoheMapKey,tablejiaoheShiti);
				var tableTr = handleDataTableJiaohe(tablejiaoheMapKey,item.showTableValidationFormula);
				$("#biaojianValidationFormulaTable").append(tableTr);
			});
		}
	});
}

//表间关系校核实体(展示公式，数据库后台存的公式，公式类型)
function TablejiaoheShiti(tableGongshiShow,tableGongshiReal,jiaohetype){
	this.tableGongshiShow=tableGongshiShow;
	this.tableGongshiReal=tableGongshiReal;
	this.jiaohetype=jiaohetype;
}

function handleDataTableJiaohe(tablejiaoheMapKey,tableGongshiShow){
	var tr = '<tr>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + tableGongshiShow + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2" id="' + tablejiaoheMapKey + '"><a href="#" onclick="deleteTablejiaoheTableTr(this)">删除</a>';
    tr += '<td style="display:none">' + tablejiaoheMapKey + '</td>';
    tr += '</tr>';
    return tr; 
}
function deleteTablejiaoheTableTr(obj){
	var id=$(obj).parent().attr('id');
	//删除表格里面的TR
	$('#'+id).parent().remove();
	//删除MAP里面的行计算
	tableValidationMap.remove(id);
}

function deleteATableValidationRight(obj){
	$(obj).closest('tr').remove();
}
